<template>
  <div class="register-container">
    <el-card class="register-card">
      <h2>学生注册</h2>
      <el-form
        ref="registerForm"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="姓名" prop="realName">
          <el-input v-model="form.realName" />
        </el-form-item>
        
        <el-form-item label="学号" prop="idNumber">
          <el-input v-model="form.idNumber" />
        </el-form-item>
        
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="form.password"
            type="password"
            show-password
          />
        </el-form-item>
        
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input
            v-model="form.confirmPassword"
            type="password"
            show-password
          />
        </el-form-item>
        
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" />
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" @click="handleRegister">注册</el-button>
          <el-link
            type="primary"
            class="login-link"
            @click="$router.push('/login')"
          >
            返回登录
          </el-link>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { reactive,ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { register } from '@/api/auth'

const router = useRouter()

const registerForm = ref(null)
const form = ref({
  realName: '',
  idNumber: '',
  password: '',
  confirmPassword: '',
  email: ''
})



const validatePass = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请输入密码'))
  } else {
    if (form.value.confirmPassword !== '') {
      if (form.value.password !== form.value.confirmPassword) {
        callback(new Error('两次输入密码不一致'))
      }
    }
    callback()
  }
}

const rules = {
  realName: [
    { required: true, message: '请输入姓名', trigger: 'blur' },
    { 
      pattern: /^[\u4e00-\u9fa5a-zA-Z]{2,20}$/, 
      message: '姓名必须为2-20个汉字或字母', 
      trigger: 'blur' 
    }
  ],
  idNumber: [
    { required: true, message: '请输入学号', trigger: 'blur' },
    { 
      pattern: /^[a-zA-Z0-9]{6,20}$/, 
      message: '账号必须为6-20个字母或数字', 
      trigger: 'blur' 
    }
  ],
  password: [
    { required: true, validator: validatePass, trigger: 'blur' },
    { 
      min: 6, 
      max: 20, 
      message: '密码长度必须在6到20个字符之间', 
      trigger: 'blur' 
    }
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    { 
      min: 6, 
      max: 20, 
      message: '密码长度必须在6到20个字符之间', 
      trigger: 'blur' 
    }
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' },
    { 
      pattern: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/, 
      message: '请输入正确的邮箱格式', 
      trigger: 'blur' 
    }
  ]
}

const handleRegister = async () => {
   // 使用 registerForm 来访问表单
   if (registerForm.value) {  
    try {
      // 触发表单验证
      await registerForm.value.validate();
      // 实现注册逻辑
      const res = await register(form.value)
      ElMessage.success('注册成功');
      // router.push('/login');
      // 通过路由参数传递学号
      router.push({
        path: '/login',
        query: {
          idNumber: form.value.idNumber,
          password: form.value.password
        }
      });
    } catch (error) {
      // 验证失败，显示错误信息
      console.log(error);
      
      ElMessage.error('注册失败');
    }
  } else {
    ElMessage.error('表单引用无效');
  }
}
</script>

<style scoped>
.register-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f5f7fa;
}

.register-card {
  width: 500px;
}

.login-link {
  margin-left: 10px;
}
</style> 